﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Controls;
using com.skype.api;

namespace Hugo
{
    public class Util
    {

        [System.Runtime.InteropServices.DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        public static BitmapSource getBitmapSource(System.Drawing.Bitmap source)
        {
            IntPtr ip = source.GetHbitmap();
            BitmapSource bs = null;
            try
            {
                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
                   IntPtr.Zero, Int32Rect.Empty,
                   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(ip);
            }

            return bs;
        }

        public static Rectangle getSymbol(BitmapSource u)
        {
            return getSymbol(u,GlobalSettings.ICON_WIDTH_SELECTED,GlobalSettings.ICON_HEIGHT_SELECTED);
        }

        private static int maxWidth = GlobalSettings.ICON_WIDTH_SELECTED;
        private static int maxHeight = GlobalSettings.ICON_HEIGHT_SELECTED;

        public static Rectangle getUnCenteredSymbol(BitmapSource u, int height, int width)
        {
            double xscale = ((double)maxWidth) / u.Width;
            double yscale = ((double)maxHeight) / u.Height;

            double scale = Math.Min(xscale, yscale);

            Rectangle item = new Rectangle();
            item.Height = u.Height * scale;
            item.Width = u.Width * scale;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            myBrush.ImageSource = u;
            item.Fill = myBrush;

            return item;
        }

        public static Rectangle getSymbol(BitmapSource u, int height, int width)
        {
            double xscale = ((double)maxWidth) / u.Width;
            double yscale = ((double)maxHeight) / u.Height;

            double scale = Math.Min(xscale, yscale);

            Rectangle item = new Rectangle();
            item.Height = u.Height * scale;
            item.Width = u.Width * scale;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            myBrush.ImageSource = u;
            item.Fill = myBrush;

            if (scale == xscale)
            {
                item.Margin = new Thickness(0, (maxHeight - (u.Height * scale)) / 2, 0, 0);
            }
            else
            {
                item.Margin = new Thickness((maxWidth - (u.Width * scale)) / 2, 0, 0, 0);
            }

            return item;
        }

        public static int getFontHeightFromSize(int fontSize)
        {
            System.Drawing.FontFamily ff = new System.Drawing.FontFamily("Arial");
            int ascentPixel = 
                fontSize * ff.GetCellAscent(System.Drawing.FontStyle.Regular) / ff.GetEmHeight(System.Drawing.FontStyle.Regular);
            return ascentPixel;
        }

        internal static System.Windows.Controls.Image getBitmapImage(BitmapSource source)
        {
            Image img = new Image();
            img.Source = source;// new BitmapImage(new Uri("/Resources/images/nurse.png", UriKind.RelativeOrAbsolute));
            return img;
        }

        public static Dictionary<String, BitmapSource> sourcesForContacts = new Dictionary<string, BitmapSource>();

        public static BitmapSource getImageForContact(Contact c)
        {
            if (sourcesForContacts.Keys.Contains(c.getDisplayName()))
            {
                return sourcesForContacts[c.getDisplayName()];
            }

            if (c.getAvatarImage() != null)
            {
                System.IO.MemoryStream stream = new System.IO.MemoryStream(c.getAvatarImage());
                JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
                BitmapSource source = decoder.Frames[0];
                sourcesForContacts.Add(c.getDisplayName(), source);
            }
            else
            {
                sourcesForContacts.Add(c.getDisplayName(), getBitmapSource(Properties.Resources.icon_skypecontact));
            }

            return sourcesForContacts[c.getDisplayName()];
        }
    }
}
